<?php

/**
 * created by zzy
 * date: 2018/1/26 14:07
 */

namespace App\Api\Service\Buyers\Controllers;

use App\Eloquent\Ygt\Buyers;
use App\Eloquent\Ygt\BuyersProduct;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class BuyersProductController extends Controller
{
    public $userId          = 25;
    public function imgList()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $buyersProductId    = request('id',0);
        if( !$userInfo || !$buyersProductId)
        {
            xThrow( ERR_PARAMETER );
        }
        $where              = ['id'=>$buyersProductId];
        $info               = BuyersProduct::where($where)->first();
        $imgIds             = '';
        $imgIdArr           = $imgPathArr = [];
        if($info)
        {
            $buyers         = $info->toArray();
            $imgIds         = $buyers['img_ids'];
            if($imgIds)
            {
                $imgIdArr   = explode(',',$imgIds);
                $imgPathArr = Func::getImgUrlByIds($imgIds);
            }
        }
        $result         = [
            'id'=>$buyersProductId,'img_id_arr'=>$imgIdArr,'img_path_arr'=>$imgPathArr
        ];
        return $result;
    }
    public function imgEdit()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $buyersProductId    = request('id',0);
        $imgIds             = request('img_ids','');
        if( !$userInfo || !$buyersProductId || !$imgIds)
        {
            xThrow( ERR_PARAMETER );
        }
        $data               = ['img_ids'=>$imgIds];
        $where              = ['id'=>$buyersProductId];
        $updateResult       = BuyersProduct::where($where)->update($data);
        if($updateResult === false)
        {
            xThrow(ERR_UNKNOWN);
        }
        return ['message'=>'编辑成功'];
    }
}